package com.bicon.botu.finance.payment.rpc.service;

import com.bicon.botu.core.base.common.entity.BaseResult;
import com.bicon.botu.finance.payment.common.PaymentConstants;
import com.bicon.botu.finance.payment.entity.AppPaymentInfoEntity;
import com.bicon.botu.finance.payment.service.AppPaymentInfoService;
import com.bicon.botu.finance.payment.service.IRefundService;
import com.bicon.botu.finance.payment.service.PayService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 支付rpc
 * @author tandk
 * @date 2017/9/15 0015 上午 9:50
 */
@Service
public class PaymentRpcService {

    @Autowired
    private AppPaymentInfoService appPaymentInfoService;

    @Autowired
    private PayService payService;

    @Autowired
    private IRefundService refundService;

    /**
     * 列出该APP支持的支付方式
     *
     * @param code
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public BaseResult listPay(@RequestParam String code){
        return appPaymentInfoService.listPay(code);
    }

    /**
     * SDK生成APP支付订单信息
     *
     * @param code        app code
     * @param orderNum    订单号
     * @param subjectName 支付标题
     * @param body        支付信息体 微信支付时，可以忽略
     * @param money       支付金额
     * @param type        支付类型
     * @param attr        扩展字段，传入什么，支付成功后传回什么
     */
    @RequestMapping(value = "/launchPay", method = RequestMethod.POST)
    public BaseResult launchPay(@RequestParam(value = "code") String code, @RequestParam(value = "orderNum")  String orderNum,
                                @RequestParam(value = "subjectName")  String subjectName, @RequestParam(value = "body") String body,
                                @RequestParam(value = "money")  String money, @RequestParam(value = "type")  String type,
                                @RequestParam(value = "attr") String attr) {
        // 根据code查询支付所需配置信息
        AppPaymentInfoEntity appPaymentInfoEntity = appPaymentInfoService.findOneByCode("BOTU");
        if (null == appPaymentInfoEntity){
            return new BaseResult(PaymentConstants.RESPONSE_CODE_400, "此code无对应支付参数信息", null);
        }
        return payService.launchPay(appPaymentInfoEntity,orderNum,subjectName,body,money,type,attr);
    }

    /**
     * 申请退款订单信息
     * @param code          应用code
     * @param orderNum      订单编号
     * @param money         金额
     * @param type          支付类型
     * @return
     */
    @RequestMapping(value = "/refund", method = RequestMethod.POST)
    public BaseResult refund(@RequestParam(value = "code") String code,
                             @RequestParam(value = "orderNum")  String orderNum,
                             @RequestParam(value = "money")  String money,
                             @RequestParam(value = "type")  String type,
                             @RequestParam(value = "refundNo")  String refundNo,
                             @RequestParam(value = "refundReason")  String refundReason,
                             @RequestParam(value = "orderMoney")  String orderMoney) {
        // 根据code查询支付所需配置信息
        AppPaymentInfoEntity appPaymentInfoEntity = appPaymentInfoService.findOneByCode("BOTU");
        if (null == appPaymentInfoEntity){
            return new BaseResult(PaymentConstants.RESPONSE_CODE_400, "此code无对应支付参数信息", null);
        }
        return refundService.refund(appPaymentInfoEntity, type, orderNum, money, refundNo, refundReason,orderMoney);
    }

}
